package jdbc.demo.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 描述:
 *
 * @author Zhanggq
 * @date 2019/9/17 10:22
 */
public class JdbcTool2 {

    private static Properties p = new Properties();

    static {
        try {
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
            p.load(inputStream);
            String driverClassName = p.getProperty("driverClassName");
            Class.forName(driverClassName);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            String url = p.getProperty("url");
            String username = p.getProperty("username");
            String password = p.getProperty("password");
            return DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
